﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

namespace WebApp.Controls
{
    public partial class HotProduct_L : System.Web.UI.UserControl
    {

        private ProductManager m_ProductManager = new ProductManager();

        //private int m_ShowCount = 60;
        //public int ShowCount
        //{
        //    get { return m_ShowCount; }
        //    set { m_ShowCount = value; }
        //}

        private Guid m_CategoryId;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindProduct();
            }
        }

        private void BindProduct()
        {
            string m_Order = Request.QueryString["order"];
            Guid m_CategoryId = new Guid(Request.QueryString["CID"].ToString()); 
            List<ProductInfo> theProductList = this.m_ProductManager.GetProductInfoByFirstCategory(m_CategoryId).OrderByDescending(theProduct => theProduct.Views).ToList();

            switch (m_Order)
            {
                case "PA":
                    theProductList = theProductList.OrderBy(theProduct => theProduct.网络零售价).ToList();
                    break;
                case "PD":
                    theProductList = theProductList.OrderByDescending(theProduct => theProduct.网络零售价).ToList();
                    break;
                case "CA":
                    theProductList = theProductList.OrderBy(theProduct => theProduct.PublishDate).ToList();
                    break;
                case "CD":
                    theProductList = theProductList.OrderByDescending(theProduct => theProduct.PublishDate).ToList();
                    break;
                case "VD":
                default:
                    theProductList = theProductList.OrderByDescending(theProduct => theProduct.Views).ToList();
                    //List<Cosmetic.DataEntity.Product> theTempList = (from theProduct in theProductList
                    //                                                 where theProduct.Comments.Count > 0
                    //                                                 orderby theProduct.Comments.Average(theComment => theComment.Score) descending
                    //                                                 select theProduct).ToList();
                    //theProductList = theTempList.Union(from theProduct in theProductList
                    //                                   where theProduct.Comments.Count == 0
                    //                                   select theProduct).ToList();
                    break;
            }
            this.lvProduct.DataSource = theProductList;
            this.lvProduct.DataBind();
        }

        protected void lvProduct_PagePropertiesChanged(object sender, EventArgs e)
        {

            this.BindProduct();
        }
    }
}